﻿package Model;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class HoaDon {
	//private int maHoaDon;
	private int iduser;
	private GioHang gioHang;
	private Date ngayLapHoaDon;
	private String ptThanhToan;
	private ThongTinNguoiNhan thongTinNguoiNhan;
	private String trangThai;
	
	public HoaDon(){
		super();
	}
	
public HoaDon(int iduser, GioHang gioHang, Date ngayLapHoaDon,
			String ptThanhToan, ThongTinNguoiNhan thongTinNguoiNhan,
			String trangThai) {
		super();
		this.iduser = iduser;
		this.gioHang = gioHang;
		this.ngayLapHoaDon = ngayLapHoaDon;
		this.ptThanhToan = ptThanhToan;
		this.thongTinNguoiNhan = thongTinNguoiNhan;
		this.trangThai = trangThai;
	}


public void saveOrder() {
    Connection con = ConnectDatabase.getConnection();
    try {
        PreparedStatement sta = con.prepareStatement("INSERT INTO `order`(userid,order_date,order_status,"
                + "payment_name,payment_provinceid,payment_address,payment_phone,payment_cost,payment_idCard,"
                + "note,payment_method,delivery) VALUES(?,?,?,?,?,?,?,?,?,?,?,?)");
        sta.setInt(1, iduser);
        java.util.Date today = new java.util.Date();
        java.sql.Date todayInSQL = new Date(today.getTime());
        sta.setDate(2, todayInSQL);
        sta.setString(3, "Chưa giao hàng");
        sta.setString(4, thongTinNguoiNhan.getThongTinUser().getHoTen());
        sta.setInt(5, thongTinNguoiNhan.getThongTinUser().getTinh());
        sta.setString(6, thongTinNguoiNhan.getThongTinUser().getDiaChi());
        sta.setString(7, thongTinNguoiNhan.getThongTinUser().getDienThoai());
        sta.setDouble(8, gioHang.getSum());
        sta.setString(9, ngayLapHoaDon.toString());
        sta.setString(10, thongTinNguoiNhan.getGhiChu());
        sta.setString(11, "Tien Mat");
        sta.setString(12, "Giao Hang Tan Noi");
        sta.executeUpdate();
        PreparedStatement sta1 = con.prepareStatement("insert INTO mobileorder(mobileid,orderid,order_quantity)"
                + " VALUES(?,(select orderid from `order` order by orderid desc limit 1),?)");
        PreparedStatement sta2 = con.prepareStatement("update mobile set quantity = (quantity - ?) where mobileid =  ?");
        for (Mobile lineMb : gioHang.getGioHang()) {
            sta1.setInt(1, lineMb.getMobileID());
            sta1.setInt(2, lineMb.getSoLuong());
            sta1.executeUpdate();
            sta2.setInt(1, lineMb.getSoLuong());
            sta2.setInt(2, lineMb.getMobileID());
            sta2.executeUpdate();
        }

        con.close();
    } catch (SQLException ex) {
        ex.printStackTrace();
    }
}








	//	public int getMaHoaDon() {
//		return maHoaDon;
//	}
//	public void setMaHoaDon(int maHoaDon) {
//		this.maHoaDon = maHoaDon;
//	}
	public GioHang getGioHang() {
		return gioHang;
	}
	public int getIduser() {
		return iduser;
	}

	public void setIduser(int iduser) {
		this.iduser = iduser;
	}

	public void setGioHang(GioHang gioHang) {
		this.gioHang = gioHang;
	}
	public Date getNgayLapHoaDon() {
		return ngayLapHoaDon;
	}
	public void setNgayLapHoaDon(Date ngayLapHoaDon) {
		this.ngayLapHoaDon = ngayLapHoaDon;
	}
	public String getPtThanhToan() {
		return ptThanhToan;
	}
	public void setPtThanhToan(String ptThanhToan) {
		this.ptThanhToan = ptThanhToan;
	}
	public ThongTinNguoiNhan getThongTinNguoiNhan() {
		return thongTinNguoiNhan;
	}
	public void setThongTinNguoiNhan(ThongTinNguoiNhan thongTinNguoiNhan) {
		this.thongTinNguoiNhan = thongTinNguoiNhan;
	}
	public String getTrangThai() {
		return trangThai;
	}
	public void setTrangThai(String trangThai) {
		this.trangThai = trangThai;
	}
	
	

}
